package com.example.controll.imgControll;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.example.config.R;
import com.example.domain.childer_articles;
import com.example.domain.lease_review;
import com.example.domain.news;
import com.example.service.img.imgService;
import com.example.service.news.newService;
import org.springframework.boot.autoconfigure.web.servlet.MultipartAutoConfiguration;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.UUID;

@RestController
@RequestMapping("/img")
public class img {

    @Resource
    private imgService imgService;

    @Resource
    private newService newService;

    @RequestMapping("/insert")
    private R insert(lease_review lease_review){
        System.out.println(lease_review);
//        lease_review.setImgurl1('"'+lease_review.getImgurl1()+'"');
        boolean shangchuan = imgService.shangchuan(lease_review);
//        boolean shangchuan=false;
        return new R(shangchuan,shangchuan?"上传成功":"上传失败");
    }

    @RequestMapping("/upload")
    public String upload(String name, MultipartFile mFile, HttpServletRequest request){
        System.out.println("aaaaaa");
        MultipartFile mFile1 = mFile;
//        String s= tupian(mFile);
//        if(s.length()>=0) {
            String s = tupian1(mFile1);
//
        System.out.println(s);
        return s;
    }
    @RequestMapping("/upload1")
    public String upload1(String name, MultipartFile mFile, HttpServletRequest request){
        System.out.println("aaaaaa");
        MultipartFile mFile1 = mFile;
//        String s= tupian(mFile);
//        if(s.length()>=0) {
        String s = tupian(mFile1);
//
        System.out.println(s);
        return s;
    }


    public String tupian(MultipartFile mFile){
        String fileName= UUID.randomUUID()+"-"+mFile.getOriginalFilename();
        boolean flag;
        String filepath ="F:\\毕业设计\\代码\\xiaochengxu\\tupian\\后台\\儿童用品img\\14\\";
        File dir = new File(filepath);
        if(!dir.exists()){//如果这个文件的路径不存在
            //则创建这个目录
            dir.mkdirs();
        }
//        System.out.println(fileName);
        File destFileName = new File(dir + File.separator + fileName);
        try{
            //4.把上传的图片存储到服务器中的文件
            mFile.transferTo(destFileName);
        } catch (IOException e) {
            e.printStackTrace();
            flag=false;
        }
//        String s=destFileName.getPath();
//        return s;
//        tupian1(mFile);
        return fileName;
    }

    public String tupian1(MultipartFile mFile){
        String fileName= UUID.randomUUID()+"-"+mFile.getOriginalFilename();
        boolean flag;
        String filepath ="F:\\毕业设计\\代码\\wangye\\student_status_management_vue-master\\src\\assets\\tupian\\后台\\儿童用品img\\14\\";
        File dir = new File(filepath);
        if(!dir.exists()){//如果这个文件的路径不存在
            //则创建这个目录
            dir.mkdirs();
        }
        System.out.println(fileName);
        File destFileName = new File(dir + File.separator + fileName);
        try{
            //4.把上传的图片存储到服务器中的文件
            mFile.transferTo(destFileName);
        } catch (IOException e) {
            e.printStackTrace();
            flag=false;
        }
//        String s=destFileName.getPath();
//        return s;
        return fileName;
    }

    @RequestMapping("/select")
    private R selectall(int a,int b){
        return new R(true,imgService.findall(a,b));
    }

    @RequestMapping("/name1")
    private R findByname(lease_review review, int currentPage, int pageSize){

//        getPage(currentPage, pageSize,articles);
//        IPage<Book> page = bookService.getPage(currentPage, pageSize,book);
        IPage<lease_review> page = imgService.getPagebyname(currentPage,pageSize,review);
        //如果当前页码值大于了总页码值，那么重新执行查询操作，使用最大页码值作为当前页码值
        if( currentPage > page.getPages()){
            page = imgService.getPagebyname((int)page.getPages(), pageSize,review);
        }

//        IPage<childer_articles> page = articlesService.getPage(currentPage, pageSize,articles);

        return new R(true,page);
    }

//    删除
//    @RequestMapping("/deletebyid")
//    private R delete(int id){
//        boolean b = imgService.deletelease_review(id);
//        return new R(b,b?"删除成功":"删除失败");
//    }

    @RequestMapping("/deletebyid")
    private R delete(int id){
        lease_review review = imgService.selectBYid(id);
        news news1 = new news();
        news1.setXiaoxi("您的儿童用品没有通过审核");
        news1.setUserid(review.getUserid());
        news1.setType("平台");
        Date date = new Date();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        System.out.println(simpleDateFormat.format(date));
        String format = simpleDateFormat.format(date);
        news1.setTime(format);
        newService.insertnews(news1);
        boolean b = imgService.deletelease_review(id);
//        boolean b = true;
        return new R(b,b?"删除成功":"删除失败");
    }

}
